// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.apps.moderator;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import org.json.JSONObject;

/**
 * A ListEntry for the Moderator application. All entries are derived from
 * JSONObjects so this is stored in common.
 */
public abstract class ModeratorListEntry implements ListEntry {

  final JSONObject jsonObject;

  ModeratorListEntry(JSONObject jsonObject) {
    this.jsonObject = jsonObject;
  }

  public void addContextItems(ContextMenu menu, final Activity activity) {
    menu.add(R.string.share).setOnMenuItemClickListener(new OnMenuItemClickListener() {
      public boolean onMenuItemClick(MenuItem item) {
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, getUriString());

        try {
          activity.startActivity(
              Intent.createChooser(intent, activity.getText(R.string.choose_action)));
        } catch (android.content.ActivityNotFoundException ex) {

        }
        return true;
      }
    });

    menu.add(R.string.open_in_website).setOnMenuItemClickListener(new OnMenuItemClickListener() {
      public boolean onMenuItemClick(MenuItem item) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(getUriString()));
        activity.startActivity(intent);
        return true;
      }
    });
  }

  /**
   * Provides a web URI for this item that refers to the same item, on the main
   * Moderator website.
   */
  protected abstract String getUriString();

  public boolean clicked(Activity activity) {
    // Default is nothing happens on click
    return false;
  }

}
